package com.shyroke.daydayzhuanweb.controller;

import com.google.gson.Gson;
import com.shyroke.daydayzhuanapi.dto.AppDto;
import com.shyroke.daydayzhuanapi.dto.NBStep;
import com.shyroke.daydayzhuansvc.service.impl.AppService;
import com.shyroke.daydayzhuansvc.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;


/**
 * 类名 ：后台用户controller
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/11 10:46
 */
@Controller
@RequestMapping(value = "admin/app")
public class AdminAppController extends  BaseController{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppService appService;


    /**
     * 跳转到牛帮首页大厅页面
     * @param model
     * @return
     */
    @RequestMapping(value = "niubang/index")
    public ModelAndView niubangIndex(ModelAndView model){

        model.setViewName("admin/app/niubang/index");
        return  model;
    }

    /**
     * 获取牛帮首页大厅数据
     * @return
     */
    @RequestMapping(value = "niubang/getIndex")
    @ResponseBody
    public TabelData<Map<String,String>> niubangGetIndex(Integer page,String key,String projectName){
        TabelData tabelData = new TabelData();
        if(page == null){
            page = 1;
        }
        Map<String,String> param = new HashMap<>();

        try {
            if(!StringUtils.isEmpty(key)){
                //按关键字搜索
                param.put("id","yz_task:index");
                param.put("ac","search");
                param.put("api","1");
                param.put("mobile","2");
                param.put("m","byKeyword");
                param.put("text",key);

            }else if(!StringUtils.isEmpty(projectName)){
                //按项目名称搜索
                param.put("id","yz_task:index");
                param.put("ac","search");
                param.put("api","1");
                param.put("mobile","2");
                param.put("m","byClass");
                param.put("text",projectName);
            }else{
                //首页大厅
                param.put("id","yz_task:index");
                param.put("ac","list");
                param.put("api","1");
                param.put("mobile","2");
                param.put("page",page.toString());
            }

            String result =  HttpClientUtils.doPost("http://api.yuzhuan.com/plugin.php",param,"UTF-8");
            Gson gson = new Gson();
            List<Map<String,String>> dataList = new ArrayList<>();
            dataList = gson.fromJson(result,List.class);
            for(Map<String,String> map:dataList){
                String dataline = map.get("dateline");
                if(dataline == null){
                    continue;
                }
                map.put("createTime", DateUtils.getTimestampDate(dataline+"000"));
            }
            tabelData.setCount(500);
            tabelData.setData(dataList);

        }catch (Exception e){
            logger.info("牛帮app",e);
            return null;
        }

        return tabelData;
    }


    @RequestMapping(value = "niubang/desc/{tid}")
    public ModelAndView niubangDesc(@PathVariable("tid")Integer tid,ModelAndView model) {
        if(tid == null){
            return model;
        }

        Map<String,String> param = new HashMap<>();
        param.put("id","yz_task:index");
        param.put("ac","view");
        param.put("api","1");
        param.put("mobile","2");
        param.put("tid",tid.toString());

        try {
            String result = HttpClientUtils.doPost("http://api.yuzhuan.com/plugin.php",param,"UTF-8");
            Gson gson = new Gson();
            Map<String,Object> map = new HashMap<>();
            map = gson.fromJson(result,Map.class);

            List<com.google.gson.internal.LinkedTreeMap> steps = (List<com.google.gson.internal.LinkedTreeMap>)map.get("step");
            List<NBStep> nbStepList = new ArrayList<>();
            NBStep nbStep ;
            if(!CollectionUtils.isEmpty(steps)){
                for(com.google.gson.internal.LinkedTreeMap map2:steps){
                    nbStep = new NBStep();
                    Object picObj = map2.get("pic");
                    if(picObj != null){
                        String pic = picObj.toString();
                        if(!pic.startsWith("http")){
                            pic = "http://task.yuzhuan.com/"+pic;
                        }
                        nbStep.setPic(pic);
                    }
                    nbStep.setUrl(map2.get("url")==null?null:map2.get("url").toString());
                    nbStep.setStepName(map2.get("stepName")==null?null:map2.get("stepName").toString());
                    nbStep.setStepType(map2.get("stepType")==null?null:map2.get("stepType").toString());
                    nbStep.setTitle(map2.get("title")==null?null:map2.get("title").toString());
                    nbStepList.add(nbStep);
                }
            }

             model.addObject("stepList",nbStepList);
             model.setViewName("admin/app/niubang/desc");
        }catch (Exception e){
            logger.info("牛帮app",e);
        }

        return  model;
    }

    @GetMapping(value = "list")
    @ResponseBody
    public List<AppDto> appList(){
        return appService.appList();
    }

}
